import os
import shutil
from shutil import make_archive, unpack_archive

archive_name = 'demo'
dir_name = 'demo_dir'
# 1.归档 tar.gz
ret = make_archive(archive_name, 'gztar', dir_name)
'demo.tar.gz'

# 2.解档
ret = unpack_archive(archive_name + '.tar.gz', 'unpack_dir')
'unpack_dir/'

# 3.支持的归档格式
ret = shutil.get_archive_formats()
print(ret)
"""
[('bztar', "bzip2'ed tar-file"), 
('gztar', "gzip'ed tar-file"), 
('tar', 'uncompressed tar file'), 
('xztar', "xz'ed tar-file"), 
('zip', 'ZIP file')]
"""

# 4.注册zstd格式归档 --NG
""" 
def pack_zstd(source, dest):
    cmd = f'tar -I zstd -cf {dest}.tar.zst {source}'
    cmd = f'zstd {source} -o {dest}.tar.zst'
    os.system(cmd)

def unpack_zstd(source, dest):
    cmd = f'tar -I zstd -xf {source} -C {dest}'
    cmd = f'zstd -d {source} -o {dest}'
    os.system(cmd)

shutil.register_archive_format('zst', pack_zstd)
shutil.register_unpack_format('zst', ['.tar.zst'], unpack_zstd)
ret = make_archive(archive_name, 'zst', dir_name) 
"""